package fourfaith.com.ff_sl21_app.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import fourfaith.com.ff_sl21_app.R;
import fourfaith.com.ff_sl21_app.utils.Constants;


public class HistoryDetailListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    // 设置底部布局
    private static final int TYPE_FOOTER = 0;
    // 设置默认布局
    private static final int TYPE_DEFAULT = 1;

    // 上下文
    private Context context;
    private LayoutInflater mInflater;
    // 数据源
    private List<Map<String,Object>> items ;
    private RecyclerView recyclerView;
    private View viewRecycler;

    // 判断是不是最后一个item，默认是true
    private boolean mShowFooter = true;
    private int dimensionPixelSize = 0;

    public HistoryDetailListAdapter(Context context, List<Map<String,Object>> items, RecyclerView recyclerViews) {
        this.context = context;
        this.items = items;
        this.mInflater = LayoutInflater.from(context);
        dimensionPixelSize = context.getResources().getDimensionPixelSize(R.dimen.bottom_item_height);
        this.recyclerView = recyclerViews;
    }

    // 设置数据并刷新
    public void setData(List<Map<String,Object>> Data)
    {
        this.items = Data;
        this.notifyDataSetChanged();
    }

    // 设置不同的item
    @Override
    public int getItemViewType(int position)
    {
        // 判断当前位置+1是不是等于数据总数（因为数组从0开始计数），是的就加载底部布局刷新，不是就加载默认布局
        if (position + 1 == getItemCount())
        {
            return TYPE_FOOTER;
        } else
        {
            return TYPE_DEFAULT;
        }
    }


    // 设置布局（相当于设置convertView）
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {

        if (viewType == TYPE_DEFAULT)
        {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_history_detail_list_item, parent, false);

            DefaultViewHolder vh = new DefaultViewHolder(v);
            v.setTag(vh);
            return vh;
        } else
        {
            // 实例化布局
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_footer, null);
            // 代码实现加载布局
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            return new FooterViewHolder(view);
        }
    }

    // 在布局中加载数据（绑定数据）
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        // 用来在运行时指出对象是某一个对象
        if (holder instanceof DefaultViewHolder)
        {
            Map<String,Object> item = items.get(position);
            if (item == null)
            {
                return;
            }

            //数据填充
            ((DefaultViewHolder) holder).tm.setText(Constants.getString(item.get("tm")));

            List<String> viewList = (List<String>) item.get("viewList");
            List<String> valueList = (List<String>) item.get("valueList");
            LinearLayout rootLinear = ((DefaultViewHolder) holder).rootLinear;
            rootLinear.removeAllViews();
            LinearLayout playout = new LinearLayout(context);

            for (int i=0; i<viewList.size();i++){

                LinearLayout layout = new LinearLayout(context);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                layoutParams.height=Constants.dip2px(context,40);
                layout.setLayoutParams(layoutParams);
                TextView fname = new TextView(context);
                TextView fvalue = new TextView(context);

                fname.setLayoutParams(layoutParams);
                fname.setTextColor(ContextCompat.getColorStateList(context, R.color.itemLeftColor));
                fname.setTextSize(14);
                fname.setText(viewList.get(i)+"：");
                fname.setGravity(Gravity.CENTER|Gravity.END);

                layout.addView(fname);

                fvalue.setLayoutParams(layoutParams);
                fvalue.setTextColor(ContextCompat.getColorStateList(context, R.color.itemRightColor));
                fvalue.setTextSize(14);
                fvalue.setText(valueList.get(i));
                fvalue.setGravity(Gravity.CENTER|Gravity.START);
                layout.addView(fvalue);

                playout.addView(layout);
                if(i%2==1 || i==viewList.size()-1){

                    if(i%2==0 && i==viewList.size()-1){
                        //插入空布局
                        LinearLayout layout2 = new LinearLayout(context);
                        layout2.setLayoutParams(layoutParams);
                        playout.addView(layout2);
                    }

                    rootLinear.addView(playout);

                    playout = new LinearLayout(context);

                    LinearLayout.LayoutParams playoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                    playoutParams.height=Constants.dip2px(context,40);
                    playout.setLayoutParams(playoutParams);
                    playout.setOrientation(LinearLayout.HORIZONTAL);
                }


            }
        }
    }

    // 获取数据数目
    @Override
    public int getItemCount()
    {
        // 判断是不是显示底部，是就返回1，不是返回0
        int begin = mShowFooter? 1 : 0;
        // 没有数据的时候，直接返回begin
        if (items == null)
        {
            return begin;
        }
        // 因为底部布局要占一个位置，所以总数目要+1
        return items.size() + begin;
    }


    // 设置是否显示底部加载提示（将值传递给全局变量）
    public void isShowFooter(boolean showFooter)
    {
        this.mShowFooter = showFooter;
    }

    // 判断是否显示底部，数据来自全局变量
    public boolean isShowFooter()
    {
        return this.mShowFooter;
    }


    // 底部布局的ViewHolder
    public class FooterViewHolder extends RecyclerView.ViewHolder
    {
        public FooterViewHolder(View view)
        {
            super(view);
        }
    }

    // 默认布局的ViewHolder
    public class DefaultViewHolder extends RecyclerView.ViewHolder
    {
        public TextView tm;
        public LinearLayout rootLinear;

        public DefaultViewHolder(View itemView)
        {
            super(itemView);
            tm = (TextView) itemView.findViewById(R.id.tm);
            rootLinear = (LinearLayout)itemView.findViewById(R.id.rootLinear);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
